/*
 * @Author: hrg
 * @Date: 2024-01-27 13:59:57
 * @LastEditTime: 2024-01-27 14:27:45
 * @FilePath: \study_go\study08_gin\gin_demo_02\main\main.go
 * @Description:绑定表单数据
 */

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type User struct {
	UserName string `form:"username" binding:"required"`
	PassWord string `form:"password" binding:"required"`
}

func main() {
	r := gin.New()
	r.POST("/login", Login)
	r.Run(":9090")
}

func Login(c *gin.Context) {

	var form User

	if err := c.Bind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	// 判断用户名密码是否正确：
	if form.UserName == "root" && form.PassWord == "123" {
		c.JSON(http.StatusOK, gin.H{
			"success": "登录成功",
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"error": "用户名或密码错误",
		})
	}

}
